package controlisolation.lifecyclefsm.controlcodegenerator;

import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;


public class LifeCycleFSMControlCodeGeneratorGUI extends javax.swing.JFrame {

    /**
     * Creates new form LifeCycleFSMControlCodeGeneratorGUI
     */
    public LifeCycleFSMControlCodeGeneratorGUI() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFileChooserSCXMLFile = new javax.swing.JFileChooser();
        jFileChooserOutputDirectory = new javax.swing.JFileChooser();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldOutputPackage = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldClassName = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldOutputDirectory = new javax.swing.JTextField();
        jButtonGenerateCode = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldXCXMLPath = new javax.swing.JTextField();
        jButtonChooseSCXM = new javax.swing.JButton();
        jButtonChooseOutputDirectory = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Life Cycle Control Code Generator");

        jLabel1.setText("Output Package:");

        jTextFieldOutputPackage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldOutputPackageActionPerformed(evt);
            }
        });

        jLabel2.setText("ClassName:");

        jLabel3.setText("Output Directory:");

        jButtonGenerateCode.setText("Generate Control Code");
        jButtonGenerateCode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonGenerateCodeActionPerformed(evt);
            }
        });

        jLabel4.setText("SCXML Automata File:");

        jButtonChooseSCXM.setText("Choose");
        jButtonChooseSCXM.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonChooseSCXMActionPerformed(evt);
            }
        });

        jButtonChooseOutputDirectory.setText("Choose");
        jButtonChooseOutputDirectory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonChooseOutputDirectoryActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING))
                        .addGap(31, 31, 31)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jButtonGenerateCode)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(7, 7, 7)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jTextFieldOutputDirectory)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jButtonChooseOutputDirectory))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jTextFieldClassName, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(0, 23, Short.MAX_VALUE))
                                    .addComponent(jTextFieldOutputPackage)))))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addComponent(jTextFieldXCXMLPath, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonChooseSCXM)))
                .addContainerGap(11, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(34, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextFieldXCXMLPath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonChooseSCXM))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldOutputPackage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldClassName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextFieldOutputDirectory, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonChooseOutputDirectory))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonGenerateCode)
                .addGap(23, 23, 23))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextFieldOutputPackageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldOutputPackageActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextFieldOutputPackageActionPerformed

    private void jButtonChooseSCXMActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonChooseSCXMActionPerformed
        this.jFileChooserSCXMLFile.showOpenDialog(this);
        File selectedPfile = jFileChooserSCXMLFile.getSelectedFile();
        jTextFieldXCXMLPath.setText(selectedPfile.getPath());
    }//GEN-LAST:event_jButtonChooseSCXMActionPerformed

    private void jButtonChooseOutputDirectoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonChooseOutputDirectoryActionPerformed
 
        this.jFileChooserOutputDirectory.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        this.jFileChooserOutputDirectory.showOpenDialog(this);
        File selectedPfile = this.jFileChooserOutputDirectory.getSelectedFile();
        
        this.jTextFieldOutputDirectory.setText(selectedPfile.getPath());
    }//GEN-LAST:event_jButtonChooseOutputDirectoryActionPerformed

    private void jButtonGenerateCodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonGenerateCodeActionPerformed
        LifeCycleFSMControlCodeGenerator codegen=
                new LifeCycleFSMControlCodeGenerator(
                this.jTextFieldXCXMLPath.getText(),
                this.jTextFieldClassName.getText(),
                this.jTextFieldOutputPackage.getText(),
                this.jTextFieldOutputDirectory.getText());
        codegen.init();
        codegen.generateCode();
        try{
        codegen.outputGeneratedCodeToFile();
        } catch(Exception e)
        {
          JOptionPane.showMessageDialog(null, "Error producing output file", "Error", JOptionPane.ERROR_MESSAGE);  
          return;
        } 
         JOptionPane.showMessageDialog(null, "Done", "Information", JOptionPane.INFORMATION_MESSAGE);  
        
    }//GEN-LAST:event_jButtonGenerateCodeActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(LifeCycleFSMControlCodeGeneratorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(LifeCycleFSMControlCodeGeneratorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(LifeCycleFSMControlCodeGeneratorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(LifeCycleFSMControlCodeGeneratorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new LifeCycleFSMControlCodeGeneratorGUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonChooseOutputDirectory;
    private javax.swing.JButton jButtonChooseSCXM;
    private javax.swing.JButton jButtonGenerateCode;
    private javax.swing.JFileChooser jFileChooserOutputDirectory;
    private javax.swing.JFileChooser jFileChooserSCXMLFile;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField jTextFieldClassName;
    private javax.swing.JTextField jTextFieldOutputDirectory;
    private javax.swing.JTextField jTextFieldOutputPackage;
    private javax.swing.JTextField jTextFieldXCXMLPath;
    // End of variables declaration//GEN-END:variables
}
